<?php
$isReloadable = true;
require_once("../../../pers/inc.config.php");
require_once('../../_inc/inc.session.php');
require_once('../../_inc/funct.html.php');
require_once('../../_inc/funct.getFiles.php');
require_once '_inc/funct.azioneSincro.php';
require_once '../_inc/class.wfmModulo.php';

if (reqVar('azione')=='elimina') {
	$msgPag .= azioneElimina();
}
if (reqVar('azione')=='sincro') {
	$msgPag .= azioneSincro();
}

$aCamForm = array();
require("_inc/const.campiMod.php");

$sqlSel = "SELECT m.* FROM wfm_moduli m WHERE m.id_modulo = ".reqVarInt('id');
$objDB->exec($sqlSel);
if(!$aRow = $objDB->fetch()) {
	backOnErr("id. errato");
}
$oMod = new wfmModulo(reqVarInt('id'));
if ($oMod->codModulo == "") {
	backOnErr("id. errato");
}
foreach($aCamForm as $rX => $aRX) {
	foreach($aRX as $jX => $aCX) {
		switch ($aCX[4]) {
			case 'date':
				reqDef($aCX[0],YMD2DMY($aRow[$aCX[0]]));
				break;
			case 'numero':
			case 'intero':
				reqDef($aCX[0],num2str($aRow[$aCX[0]]));
				break;
			default:
				reqDef($aCX[0],$aRow[$aCX[0]]);
				break;
		}			
	}
}
$aDefCampi = unserialize($aRow['def_campi']);

$sCmdL = "";
$sCmdL .= "<a class=\"cmd1\" href=\"javascript:confSincro();\" style=\"color:red;\">sincronizza campi</a>";
$sCmdR = "";
$sCmdR .= "<a class=\"cmd1\" href=\"pagList.php?idm=".reqvar('id')."&$sXSNQS"."\" >pagine</a>";
$sCmdR .= "&nbsp;&nbsp;&nbsp;&nbsp;";
$sCmdR .= "<a class=\"cmd1\" href=\"regList.php?idm=".reqvar('id')."&$sXSNQS"."\" >regole</a>";
$sCmdR .= "&nbsp;&nbsp;&nbsp;&nbsp;";
$sCmdR .= "<a class=\"cmd1\" href=\"ruoList.php?idm=".reqvar('id')."&$sXSNQS"."\" >ruoli</a>";
$sCmdR .= "&nbsp;&nbsp;&nbsp;&nbsp;";
$sCmdR .= "<a class=\"cmd1\" href=\"javascript:confElim();\" >elimina</a>";
$sCmdR .= "&nbsp;&nbsp;&nbsp;&nbsp;";
$sCmdR .= "<a class=\"cmd1\" href=\"modMod.php?id=".reqVarUE('id')."&$sXSNQS"."\" >modifica</a>";
$sCmdR .= cmdTorna('torna');

testaHtml('Gestione work-flow','Visualizza modulo',$msgPag,'wfma');
?>
<script type="text/javascript">
<!--
function confSincro() {
	if (confirm("Confermi la sincronizzazione dei campi ?")) {
		document.location.href = "modView.php?<?="id=".reqVarUE('id')."&azione=sincro&$sXSNQS"?>";
	} 
}
function confElim() {
	if (confirm("Confermi l'eliminazione ?")) {
		document.location.href = "modView.php?<?="id=".reqVarUE('id')."&azione=elimina&$sXSNQS"?>";
	} 
}
//-->
</script>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="left">
			<? echo $sCmdL; ?>
      </td>
      <td class="cmd1" align="right">
			<? echo $sCmdR; ?>
      </td>
   </tr>
</table>
<?=getHtmlForm('frm1','',array('enctype'=>true));?>
<?=getHtmlHidden('azione','salva');?>
<?=getHtmlHidden('cod');?>
<?=getHtmlHidden('xsn',XSN);?>
<table class="tab1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
	<?=getHtmlRigaRif();?>
   <tr>
      <td class="tit1" colspan='20' align='center'>Modulo Work-Flow</td>
   </tr>
<?
if (reqVar('id_pagina_ini')) $aVarExt['hrefPagIni'] = "pagView.php?id=".reqVar('id_pagina_ini')."&xsn=".XSN;
if (reqVar('id_pagina_vis')) $aVarExt['hrefPagVis'] = "pagView.php?id=".reqVar('id_pagina_vis')."&xsn=".XSN;
echo getHtmlCampiForm($aCamForm,'vis',$aVarExt);
?>
   <tr>
      <td class="tit1" colspan='20' align='center'>Stati</td>
   </tr>
   <tr>
      <td class="lab1" colspan='1' align='center'>
      	&nbsp;
		</td>
      <td class="lab1" colspan='9' align='center'>
      	Stato
		</td>
      <td class="lab1" colspan='10' align='center'>
      	Note
		</td>
   </tr>
<?
$sqlSel = "SELECT s.* FROM wfm_stati s WHERE s.id_modulo = ".reqVarInt('id');
$objDB->exec($sqlSel);
while($aRow = $objDB->fetch()) {
?>
   <tr>
      <td class="txt1" colspan='1' align='right'>
      	<?=$aRow['stato']?>&nbsp;
		</td>
      <td class="txt1" colspan='9' align='left'>
      	<?=$aRow['descr']?>
		</td>
      <td class="txt1" colspan='10' align='left'>
      	<?=$aRow['nota']?>
		</td>
   </tr>
<?
}
?>
   <tr>
      <td class="tit1" colspan='20' align='center'>Campi</td>
   </tr>
   <tr>
      <td class="lab1" colspan='3' align='center'>
      	Nome
		</td>
      <td class="lab1" colspan='3' align='center'>
      	Etichetta
		</td>
      <td class="lab1" colspan='3' align='center'>
      	Tipo/lun/dec
		</td>
      <td class="lab1" colspan='5' align='center'>
      	dominio
		</td>
      <td class="lab1" colspan='5' align='center'>
      	default
		</td>
      <td class="lab1" colspan='1' align='center'>
      	p.
		</td>
   </tr>
<?
$numCampi=0;
//dbg_($oMod);
foreach($aDefCampi as $sNome => $aDCX) {
	if (!isset($oMod->aCampi[$sNome])) {
		$sNM[$numCampi] = " (N)";
	} elseif (serialize($aDCX) != serialize($oMod->aCampi[$sNome])) {
		$sNM[$numCampi] = " (M)";
	} else {
		$sNM[$numCampi] = "";
	}
	reqDef("dc_nom_$numCampi",$sNome);
	reqDef("dc_lab_$numCampi",$aDCX['lab']);
	reqDef("dc_tip_$numCampi",$aDCX['tip']);
	reqDef("dc_lun_$numCampi",$aDCX['lun']);
	reqDef("dc_dec_$numCampi",$aDCX['dec']);
	reqDef("dc_dom_$numCampi",$aDCX['def']);
	reqDef("dc_dom_$numCampi",$aDCX['dom']);
	reqDef("dc_lis_$numCampi",$aDCX['lis']);
	$numCampi++;
}
for($nn=0; $nn<$numCampi; $nn++) {
?>
   <tr>
      <td class="txt1" colspan='3' align='left'>
      	<?=getHtmlVisCode("dc_nom_$nn").$sNM[$nn]?>
		</td>
      <td class="txt1" colspan='3' align='left'>
      	<?=getHtmlVisText("dc_lab_$nn")?>
		</td>
      <td class="txt1" colspan='3' align='left'>
      	<?
      	switch(reqVar("dc_tip_$nn")) {
      		case "data":
      		case "tabella_glob":
      		case "tabella_spec":
      		case "testo":
      		case "lista":
      		case "si/no":
      		case "intero":
      			echo getHtmlVisText("dc_tip_$nn");
      			break;
      		case "stringa":
      			echo getHtmlVisText("dc_tip_$nn")."(".getHtmlVisNum("dc_lun_$nn",0).")";
      			break;
      		default:
      			echo getHtmlVisText("dc_tip_$nn")."(".getHtmlVisNum("dc_lun_$nn",0).",".getHtmlVisNum("dc_dec_$nn",0).")";
      			break;
      		
      	}
      	?>
		</td>
      <td class="txt1" colspan='5' align='left'>
      	<?=getHtmlVisText("dc_dom_$nn")?>
		</td>
      <td class="txt1" colspan='5' align='left'>
      	<?=getHtmlVisText("dc_def_$nn")?>
		</td>
      <td class="txt1" colspan='1' align='left'>
      	<?=getHtmlVisText("dc_lis_$nn")?>
		</td>
   </tr>
<?
}
?>
</table>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="left">
			<? echo $sCmdL; ?>
      </td>
      <td class="cmd1" align="right">
			<? echo $sCmdR; ?>
      </td>
   </tr>
</table>
<?
echo "</form>\n";
codaHtml();
exit();

function azioneElimina() {
	global $objDB;
	$msg = "";
	$sSql = "SELECT m.id_modulo, m.cod_modulo FROM wfm_moduli m WHERE m.id_modulo = ".reqVarInt('id');
	$objDB->exec($sSql);
	$aX = $objDB->fetch();
	if ($msg != "") return $msg;
	
	$sSql = "DELETE FROM wfm_pagine WHERE id_modulo = ".reqVarInt('id');
	$objDB->exec($sSql);
	$sSql = "DELETE FROM wfm_regole WHERE id_modulo = ".reqVarInt('id');
	$objDB->exec($sSql);
	$sSql = "DELETE FROM wfm_moduli WHERE id_modulo = ".reqVarInt('id');
	$objDB->exec($sSql);
	header("location: ".urlTorna("Modulo '".$aX['cod_modulo']."' eliminato"));
	exit;
}